package com.example.administrator.himalaya.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

import com.example.administrator.himalaya.adapter.MyPagerAdapter;
import com.example.administrator.himalaya.criticallies.BeijingFragment;
import com.example.administrator.himalaya.criticallies.hebeiFragment;
import com.example.administrator.himalaya.criticallies.neimengguFragment;
import com.example.administrator.himalaya.criticallies.shanxiFragment;
import com.example.administrator.himalaya.criticallies.tianjinFragment;
import com.example.administrator.himalaya.R;

import java.util.ArrayList;
import java.util.List;

import static com.example.administrator.himalaya.R.id.tab;

public class shengActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener, View.OnClickListener {

    private TabLayout mTab;
    private ViewPager pager;
    private List<Fragment> datas;
    private MyPagerAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sheng);
        mTab = (TabLayout) findViewById(tab);
        ImageView back = (ImageView) findViewById(R.id.back);
        back.setOnClickListener(this);
        pager = (ViewPager) findViewById(R.id.pager);
        datas = new ArrayList<>();
        datas.add(new BeijingFragment());
        datas.add(new tianjinFragment());
        datas.add(new hebeiFragment());
        datas.add(new shanxiFragment());
        datas.add(new neimengguFragment());
        mTab.addOnTabSelectedListener(this);
        TabLayout.Tab tab = mTab.newTab();
        tab.setText("北京");
        mTab.addTab(tab);

        tab = mTab.newTab();
        tab.setText("天津");
        mTab.addTab(tab);

        tab = mTab.newTab();
        tab.setText("河北");
        mTab.addTab(tab);

        tab = mTab.newTab();
        tab.setText("山西");
        mTab.addTab(tab);

        tab = mTab.newTab();
        tab.setText("内蒙古");
        mTab.addTab(tab);

        mAdapter = new MyPagerAdapter(getSupportFragmentManager(), datas);
        pager.setAdapter(mAdapter);
        pager.addOnPageChangeListener(
                new TabLayout.TabLayoutOnPageChangeListener(mTab)
        );

    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        pager.setCurrentItem(tab.getPosition(),false);
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    @Override
    public void onClick(View view) {
        finish();
    }
}
